import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/common/Home.vue'

import cust from '@/components/pages/Cust.vue'
import custAdd from '@/components/pages/customer/custAdd.vue'
import custUpdate from '@/components/pages/customer/custUpdate.vue'
import custDelete from '@/components/pages/customer/custDelete.vue'
import custSelect from '@/components/pages/customer/custSelect.vue'

import order from '@/components/pages/Order.vue'
import orderDispatch from '@/components/pages/order/DispatchOrders.vue'
import orderReceive from '@/components/pages/order/ReceiveOrders.vue'

import buss from '@/components/pages/Buss.vue'
import bussAdd from '@/components/pages/bussiness/bussAdd.vue'
import bussUpdate from '@/components/pages/bussiness/bussUpdate.vue'
import bussDelete from '@/components/pages/bussiness/bussDelete.vue'
import bussSelect from '@/components/pages/bussiness/bussSelect.vue'

import inve from '@/components/pages/Inve.vue'
import inveAdd from '@/components/pages/inventory/inveAdd.vue'
import inveUpdate from '@/components/pages/inventory/inveUpdate.vue'
import inveDelete from '@/components/pages/inventory/inveDelete.vue'
import inveSelect from '@/components/pages/inventory/inveSelect.vue'

import area from '@/components/pages/Area.vue'

import people from '@/components/pages/People.vue'

import login from '@/components/common/Login.vue'

import info from '@/components/common/UserInfo.vue'
import material from '@/components/pages/Material.vue'

import { jwtDecode } from 'jwt-decode';
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login,
    },
    {
      path: '/',
      name: 'Home',
      component: Home,
      redirect: '/login',
      children: [
        {
          path: '/info',
          name: 'info',
          component: info
        }
        ,
        {
          path: '/cust',
          name: 'cust',
          component: cust
        },
        {
          path: '/custAdd',
          name: 'custAdd',
          component: custAdd
        },
        {
          path: '/custUpdate',
          name: 'custUpdate',
          component: custUpdate
        },
        {
          path: '/custDelete',
          name: 'custDelete',
          component: custDelete
        },
        {
          path: '/custSelect',
          name: 'custSelect',
          component: custSelect
        }, {
          path: '/order',
          name: 'order',
          component: order
        },
        {
          path: '/orderDispatch',
          name: 'orderDispatch',
          component: orderDispatch
        },
        {
          path: '/orderReceive',
          name: 'orderReceive',
          component: orderReceive
        },
        {
          path: '/buss',
          name: 'buss',
          component: buss
        },
        {
          path: '/bussAdd',
          name: 'bussAdd',
          component: bussAdd
        },
        {
          path: '/bussUpdate',
          name: 'bussUpdate',
          component: bussUpdate
        },
        {
          path: '/bussDelete',
          name: 'bussDelete',
          component: bussDelete
        },
        {
          path: '/bussSelect',
          name: 'bussSelect',
          component: bussSelect
        },
        {
          path: '/inve',
          name: 'inve',
          component: inve
        },
        {
          path: '/inveAdd',
          name: 'inveAdd',
          component: inveAdd
        },
        {
          path: '/inveUpdate',
          name: 'inveUpdate',
          component: inveUpdate
        },
        {
          path: '/inveDelete',
          name: 'inveDelete',
          component: inveDelete
        },
        {
          path: '/inveSelect',
          name: 'inveSelect',
          component: inveSelect
        },
        {
          path: '/area',
          name: 'area',
          component: area
        },
        {
          path: '/people',
          name: 'people',
          component: people
        },
        {
          path:'/material',
          name:'material',
          component: material
        }
      ]
    },

  ]

})

function isTokenExpired(token) {
  try {
    const decoded = jwtDecode(token);
    const exp = decoded.exp * 1000; // JWT的过期时间是以秒为单位的时间戳
    const now = Date.now();
    return now > exp;
  } catch (error) {
    return true; // 如果解析出错，认为token已过期
  }
}

router.beforeEach((to, from, next) => {
  const jwtToken = localStorage.getItem('jwtToken');

  if (to.name !== 'login' && !jwtToken) {
    alert("登录信息已过期，请登录");
    next({ name: 'login' });
  } else if (jwtToken && isTokenExpired(jwtToken)) {
    alert("登录信息已过期，请重新登录");
    localStorage.removeItem('jwtToken'); // 清除过期的JWT
    localStorage.removeItem('user_id');
    localStorage.removeItem('level')
    next({ name: 'login' });
  } else {
    next();
  }
});
router.beforeEach((to, from, next) => {
  const level1 = ['cust', 'order', 'buss'];
  const level2 = ['area', 'inve', 'people','material'];
  const level = parseInt(localStorage.getItem('level'));
  
  if (to.name !== 'login') {
    if (level !== null) {
      if (level === 1) {
        if (!level1.includes(to.name)) {
          alert("抱歉，您权限不足");
          next({ name: 'cust' });
        } else {
          next();
        }
      } else if (level === 2) {
        if (!level1.includes(to.name) && !level2.includes(to.name)) {
          alert("抱歉，您权限不足");
          next({ name: 'cust' });
        } else {
          next();
        }
      } else {
        // Handle other cases based on your requirements
        next();
      }
    } else {
      // Handle cases where level is not set
      alert("请先登录");
      next({ name: 'login' });
    }
  } else {
    next();
  }
});
export default router;